package com.zzxz.practicaltrainingsys.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.Session;
import java.util.Map;

import static com.zzxz.practicaltrainingsys.component.WebSocketServer.publicSessions;
import static com.zzxz.practicaltrainingsys.component.WebSocketServer.groupSessions;

@RestController
@RequestMapping("/list")
public class UserListController {

    @GetMapping("/public/{username}")
    public JSONObject getPublicUsername(@PathVariable("username") String username) {
        JSONObject jsonObject = new JSONObject();
        boolean isEmpty = publicSessions.isEmpty();
        jsonObject.put("isEmpty", isEmpty);
        jsonObject.put("isExist", publicSessions.containsKey(username));
        return jsonObject;
    }

    @GetMapping("/group/{inviteCode}/{username}")
    public JSONObject getGroupUsername(@PathVariable("inviteCode") String inviteCode,
                                       @PathVariable("username") String username) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Session> group = groupSessions.get(inviteCode);
        jsonObject.put("isEmpty", group == null || group.isEmpty());
        jsonObject.put("isExist", group != null && group.containsKey(username));
        return jsonObject;
    }
}
